<?php
//+----------------------------------------------------------------------
//|Author: dtt Date: 2021/9/9 0009 Time: 13:32
//+----------------------------------------------------------------------

if (!function_exists('admin_suc')) {
    if (!function_exists('layuiTabSuc')) {
        function layuiTabSuc($msg, array $data = [])
        {
            $count = $msg['count'] ?? $data['count'] ?? 0;
            if (is_array($msg)) {
                $data = $msg['data'];
                $msg  = "获取数据成功";
            } else {
                $data = $data['data'] ?? $data;
            }
            $code = 0;
            return json(compact('code', 'data', 'msg', 'count'));
        }
    }
}

if (!function_exists('sort_list_tier')) {
    /**
     * 分级排序
     *
     * @param        $data
     * @param int    $pid
     * @param string $field
     * @param string $pk
     * @param string $html
     * @param int    $level
     * @param bool   $clear
     *
     * @return array
     */
    function sort_list_tier($data, $pid = 0, $field = 'pid', $pk = 'id', $html = '|-----', $level = 1, $clear = true)
    {
        static $list = [];
        if ($clear)
            $list = [];
        foreach ($data as $k => $res) {
            if ($res[$field] == $pid) {
                $res['html'] = str_repeat($html, $level);
                $list[]      = $res;
                unset($data[$k]);
                sort_list_tier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
            }
        }
        return $list;
    }
}